package com.lzm.ydpt.mvp.presenterimpl;

import com.byt.framlib.basemvp.BasePresenter;
import com.byt.framlib.basemvp.basebean.BaseResponseBean;
import com.byt.framlib.basemvp.boby.FormBodys;
import com.byt.framlib.basemvp.exception.ApiException;
import com.byt.framlib.basemvp.listener.OnLoadingErrorListener;
import com.byt.framlib.basemvp.listener.OnLoadingProgressListener;
import com.google.gson.JsonArray;
import com.lzm.ydpt.entity.mall.GoodsOption;
import com.lzm.ydpt.entity.mall.ProductBean;
import com.lzm.ydpt.module.mall.activity.ProductDetailsActivity;
import com.lzm.ydpt.mvp.api.ProgressSubscriber;
import com.lzm.ydpt.mvp.contract.ProductDetailsContract;
import com.lzm.ydpt.mvp.modelimpl.ProductDetailsModelImpl;

import java.util.Map;

/**
 * 类名称：XhXnProductDetailsPresenterImpl
 * 类描述：代理实现
 */
public class ProductDetailsPresenterImpl extends BasePresenter implements ProductDetailsContract.Presenter {

    private ProductDetailsContract.View mView;
    private ProductDetailsContract.Model mModel;

    public ProductDetailsPresenterImpl(ProductDetailsActivity context) {
        super(context);
        this.mView = context;
        this.mModel = new ProductDetailsModelImpl();
    }

    @Override
    public void onProductDetails(FormBodys formBodys) {
        mManager.http(mModel.onProductDetails(formBodys), getLifecycleProvider(), new ProgressSubscriber<>(mContext, new OnLoadingProgressListener<ProductBean>() {
            @Override
            public void onLoadingResult(BaseResponseBean<ProductBean> entity) {
                mView.onProductDetails(entity.getData());
            }
        }, new OnLoadingErrorListener() {
            @Override
            public void onError(ApiException e, String method) {
                mView.showErrorView(e.getDisplayMessage());
            }

            @Override
            public void onErrorResult(BaseResponseBean entity, String method) {
                mView.showErrorView(entity.getMessage());
            }
        }, "onProductDetails"));
    }

    @Override
    public void onCollectionProduct(FormBodys formBodys) {
        mManager.http(mModel.onCollectionProduct(formBodys), getLifecycleProvider(), new ProgressSubscriber<>(mContext, new OnLoadingProgressListener<ProductBean>() {
            @Override
            public void onLoadingResult(BaseResponseBean<ProductBean> entity) {
                mView.onCollectionProduct(entity.getMessage(), entity.getData());
            }
        }, new OnLoadingErrorListener() {
            @Override
            public void onError(ApiException e, String method) {
                mView.showMessage(e.getDisplayMessage(), method);
            }

            @Override
            public void onErrorResult(BaseResponseBean entity, String method) {
                mView.showMessage(entity.getMessage(), method);
            }
        }, "onCollectionProduct"));
    }

    @Override
    public void onAddCartProduct(FormBodys formBodys) {
        mManager.http(mModel.onAddCartProduct(formBodys), getLifecycleProvider(), new ProgressSubscriber<>(mContext, new OnLoadingProgressListener<JsonArray>() {
            @Override
            public void onLoadingResult(BaseResponseBean<JsonArray> entity) {
                mView.onAddCartProduct(entity.getMessage());
            }
        }, new OnLoadingErrorListener() {
            @Override
            public void onError(ApiException e, String method) {
                mView.showMessage(e.getDisplayMessage(), method);
            }

            @Override
            public void onErrorResult(BaseResponseBean entity, String method) {
                mView.showMessage(entity.getMessage(), method);
            }
        }, "onAddCartProduct"));
    }

    @Override
    public void onProductOption(FormBodys formBodys) {
        mManager.http(mModel.onProductOption(formBodys), getLifecycleProvider(), new ProgressSubscriber<>(mContext, new OnLoadingProgressListener<GoodsOption>() {
            @Override
            public void onLoadingResult(BaseResponseBean<GoodsOption> entity) {
                mView.onProductOption(entity.getData());
            }
        }, new OnLoadingErrorListener() {
            @Override
            public void onError(ApiException e, String method) {
                mView.showMessage(e.getDisplayMessage(), method);
            }

            @Override
            public void onErrorResult(BaseResponseBean entity, String method) {
                mView.showMessage(entity.getMessage(), method);
            }
        }, "onProductOption"));
    }
}